<!-- 推广员分包编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改推广员分包' : '添加推广员分包'"
    :visible="visible"
    width="1000px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="游戏包名称:" prop="package_id">
        <!-- <el-input-number
          :min="0"
          v-model="form.geme_id"
          placeholder="请输入游戏id"
          controls-position="right"
          class="ele-fluid ele-text-left"
        /> -->

        <PageSelect
          :placeholder="'请选择游戏包'"
          v-model="form.package_id"
          :whereName="'package_id'"
          :url="'/Package/getChannelPackage'"
          :optionLable="'package_name'"
          :optionvalue="'id'"
          :multiple="false"
          @getAllOptioin="getAllOptioin"
        />
      </el-form-item>

      <el-form-item label="推广员:" prop="admin_id">
        <!-- <el-input-number
          :min="0"
          v-model="form.admin_id"
          placeholder="请输入推广员id"
          controls-position="right"
          class="ele-fluid ele-text-left"
        /> -->
        <PageSelect
          :placeholder="'请选择推广员'"
          v-model="form.admin_id"
          :whereName="'app_name'"
          :url="'/userpromote/index'"
          :optionLable="'realname'"
          :optionvalue="'id'"
          :multiple="false"
        />
      </el-form-item>

      <el-form-item label="区别版本:" prop="sdk_version">
        <!-- <el-input-number
          :min="0"
          v-model="form.sdk_version"
          placeholder="请输入区别版本 1安卓 2苹果"
          controls-position="right"
          class="ele-fluid ele-text-left"
        /> -->
        <el-radio-group :disabled="!isUpdate" v-model="form.sdk_version">
          <el-radio
            v-for="item in redios"
            :key="item.id"
            style="display: inline-block;"
            :label="item.key"
          >
            {{ item.name }}
          </el-radio>

          <!-- <el-radio :label="1">安卓</el-radio>
          <el-radio :label="2">苹果</el-radio> -->
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
// import uploadImage from '@/components/uploadImage'
import PageSelect from "@/components/pageSelect/index.vue";
import redioRuleData from "@/utils/rules";
export default {
  name: "ApplyEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: { PageSelect },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      gameAll: [],
      // 表单验证规则
      rules: {
        package_id: [
          { required: true, message: "请输入游戏包名称", trigger: "blur" }
        ],
        admin_id: [
          { required: true, message: "请输入推广员id", trigger: "blur" }
        ],
        apply_time: [
          { required: true, message: "请输入申请时间", trigger: "blur" }
        ],
        status: [
          {
            required: true,
            message: "请输入审核状态:0=待审核,1=已审核",
            trigger: "blur"
          }
        ],
        enable_status: [
          {
            required: true,
            message:
              "请输入操作状态:0=未打包,1=打包完成,2=准备打包,-1=打包失败",
            trigger: "blur"
          }
        ],
        pack_url: [
          { required: true, message: "请输入游戏包地址", trigger: "blur" }
        ],
        sdk_version: [
          {
            required: true,
            message: "请输入区别版本 1安卓 2苹果",
            trigger: "blur"
          }
        ],
        plist_url: [
          { required: true, message: "请输入plist_url", trigger: "blur" }
        ],
        dispose_id: [
          { required: true, message: "请输入操作人", trigger: "blur" }
        ],
        dispose_time: [
          { required: true, message: "请输入操作时间", trigger: "blur" }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      redios: redioRuleData()
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    },
    "form.game_id"(val) {
      if (val) {
        console.log(val, this.gameAll);
        this.gameAll.forEach(item => {
          if (item.id == val) {
            this.$set(this.form, "sdk_version", item.sdk_version);
          }
        });
      }
    },
    "form.package_id"(newV, oldV) {
      if (newV != oldV) {
        console.log("!!!");
        console.log("!!!", newV, oldV);
        if (newV != undefined) {
          // 渲染类型
          console.log("this.gameAll", this.gameAll);
          this.gameAll.forEach(item => {
            if (item.id == newV) {
              console.log("item", item);
              this.$set(this.form, "sdk_version", item.sdk_version);
            }
          });
          // this.file_disabled = true;
        } else {
          console.log("不可以选则");
          // this.file_disabled = true;
        }
      } else {
        console.log("aaa");
        console.log("aaa", newV, oldV);
      }
    }
  },
  methods: {
    getAllOptioin(arr) {
      this.gameAll = arr;
    },
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          let form = {
            game_id: this.form.game_id,
            admin_id: this.form.admin_id,
            sdk_version: this.form.sdk_version
          };
          this.$http
            .post("/apply/edit", form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    }
  }
};
</script>

<style scoped></style>
